package com.exception;


import com.util.common.R;
import com.util.common.RStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.xml.bind.ValidationException;
import java.util.List;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    /**
     * 参数错误异常
     */
    @ExceptionHandler({IllegalArgumentException.class})
    public R handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数错误", e);
        return R.error("参数错误：" + e.getMessage(), null);
    }
    
    /**
     * 业务异常.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler({ServiceException.class})
    public R serviceException(ServiceException e) {
        log.info("业务异常：{}", e.getMessage());
        return new R(e.getCode(), "业务异常： " + e.getMessage(), e.getData());
    }
    
    
    /**
     * 参数绑定错误.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler(BindException.class)
    public R bindException(BindException e) {
        log.info("参数异常:{}", e.getMessage());
        List<ObjectError> allErrors = e.getAllErrors();
        String defaultMessage = allErrors.get(0).getDefaultMessage();
        return R.error(RStatus.INTERNAL_SERVER_ERROR.getMessage(), defaultMessage);
    }
    
    /**
     * 参数校验异常.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R validException(MethodArgumentNotValidException e) {
        log.info("参数异常:{}", e.getMessage());
        String defaultMessage;
        FieldError fieldError = e.getBindingResult().getFieldError();
        if (fieldError == null) {
            defaultMessage = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        } else {
            defaultMessage = fieldError.getDefaultMessage();
        }
        
        return R.error(RStatus.INTERNAL_SERVER_ERROR.getMessage(), defaultMessage);
    }
    
    /**
     * DuplicateKeyException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return R.error("数据重复，请检查后提交");
    }
    
    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public R handleValidationException(ValidationException e) {
        log.error(e.getMessage(), e);
        return R.error("参数校验异常，请检查填写的信息");
    }
    
    
    @ExceptionHandler({Exception.class})
    public R handleException(Exception e) {
        log.error("系统异常", e);
        return R.error(RStatus.INTERNAL_SERVER_ERROR.getMessage() + ": " + e.getMessage(), e.toString());
    }
}
